﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using MSBTC.Model;
using MSBTC.WinFormHelpers;
using MSBTC.CrystalReport;

namespace MSBTC.ReportViewer
{
    public partial class ProjRptForm : Form
    {
        PMSContext dbcontext = new PMSContext();
        FormHelpers help = new FormHelpers();
        ProjRpt prpt = new ProjRpt();

        public ProjRptForm()
        {
            InitializeComponent();
        }

        private void crysprj_Load(object sender, EventArgs e)
        {
            if (help.PrintStat() == "nALL")
            {
                string pname = help.Pname();
                var proj = (from p in dbcontext.Projects
                            where p.Project_Name == pname
                            select new { p.Project_Name, p.Budget, p.Description, p.Location, p.Status, p.Customerid_FK, p.Projectid_PK }).ToList();

                if (proj.Count != 0)
                {
                    foreach (var itms in proj)
                    {

                        var cname = (from c in dbcontext.Customers
                                     where c.Customerid_PK == itms.Customerid_FK
                                     select c).SingleOrDefault();

                        TextObject txtobj;
                        txtobj = (TextObject)prpt.ReportDefinition.ReportObjects["txtclient"];
                        txtobj.Text = cname.Customer_Name;

                        prpt.SetDataSource(proj);
                        crysprj.ReportSource = prpt;
                        //crysprj.RefreshReport();
                        return;
                    }
                }
            }
            else if (help.PrintStat() == "ALL")
            {
                try
                {
                    TextObject txtobj = (TextObject)prpt.ReportDefinition.ReportObjects["txtclient"];
                    txtobj.Text = "None";

                    crysprj.ReportSource = prpt;
                    crysprj.RefreshReport();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString() + " Nothing to Print.", "Print", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

        }

    }
}
